import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

// 路由配置
const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/HomeView.vue'),
    meta: {
      title: '首页 - MCPRepoMarket'
    }
  },
  {
    path: '/servers',
    name: 'server-list',
    component: () => import('@/views/ServerListView.vue'),
    meta: {
      title: ' Server 列表 - MCPRepoMarket'
    }
  },
  {
    path: '/servers/internal',
    name: 'internal-server-list',
    component: () => import('@/views/ServerListView.vue'),
    props: { filter: 'internal' },
    meta: {
      title: '内部 Server 列表 - MCPRepoMarket'
    }
  },
  {
    path: '/servers/:id',
    name: 'server-detail',
    component: () => import('@/views/ServerDetailView.vue'),
    props: true,
    meta: {
      title: ' Server 详情 - MCPRepoMarket'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/LoginView.vue'),
    meta: {
      title: '登录 - MCPRepoMarket',
      public: true // 标记为公开路由，无需认证
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/RegisterView.vue'),
    meta: {
      title: '注册 - MCPRepoMarket',
      public: true
    }
  },
  {
    path: '/add-server',
    name: 'add-server',
    component: () => import('@/views/AddServerView.vue'),
    meta: {
      title: '添加 Server  - MCPRepoMarket',
      requiresAuth: true // 标记为需要认证的路由
    }
  },
  {
    path: '/edit-server/:id',
    name: 'edit-server',
    component: () => import('@/views/EditServerView.vue'),
    props: true,
    meta: {
      title: '编辑 Server  - MCPRepoMarket',
      requiresAuth: true
    }
  },
  {
    path: '/docs',
    name: 'docs',
    beforeEnter() {
      // 重定向到文档系统
      window.location.href = '/docs/';
      return false; // 阻止路由继续
    },
    meta: {
      title: '文档 - MCPRepoMarket',
      public: true
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    component: () => import('@/views/NotFoundView.vue'),
    meta: {
      title: '页面不存在 - MCPRepoMarket',
      public: true
    }
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 全局前置守卫
router.beforeEach(async (to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title as string || 'MCPRepoMarket'
  
  // 初始化认证状态
  const authStore = useAuthStore()
  await authStore.initAuth()
  
  // 检查该路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查是否已认证
    if (!authStore.isAuthenticated) {
      // 未认证，重定向到登录页面
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      // 已认证，允许访问
      next()
    }
  } else {
    // 不需要认证，直接访问
    next()
  }
})

export default router 